<?php
/**
 * 存储类 - Redis
 *
 * @version $Id: LpRedis.php 137 2013-09-27 09:12:29Z linsir123 $
 * @package Store
 */
class LpRedis
{
	/**
	 * 配置
	 *
	 * @var array
	 */
	public $conf;

	/**
	 * 实例
	 *
	 * @var Redis
	 */
	private $_i;

	/**
	 * 构造函数
	 *
	 * @param array $conf 配置
	 */
	public function __construct($conf)
	{
		///
		App::$I->getDebugger()->timerStart();

		///
		$this->conf = $conf;
		list($host, $port, $db) = $conf;
		try {
			$this->_i = new Redis();
			$this->_i->connect($host, $port)
				&& $this->_i->select($db);
		} catch (Exception $e) {
			$message = $e->getMessage();
		}

		///
		App::$I->getDebugger()->log(sprintf("[Redis] - Init: %s:%s[%s]", $host, $port, $db));

		///
		if (isset($message))
			throw new Exception($message);
	}

	/**
	 * 析构函数
	 */
	function __destruct()
	{
		$this->_i->close();
	}

	/**
	 * 重载方法(具体需要查看提供的API)
	 *
	 * @param string $method 方法名
	 * @param array $params 参数列表
	 *
	 * @throws Exception
	 * @return mixed
	 */
	public function __call($method, $params = array())
	{
		///
		App::$I->getDebugger()->timerStart();

		///
		if (! method_exists($this->_i, $method))
			throw new Exception(sprintf("[Redis] Unknown method '%s'", $method));

		$date = call_user_func_array(array($this->_i, $method), $params);

		///
		App::$I->getDebugger()->log($method . json_encode($params));

		///
		return $date;
	}
}

///
if (! class_exists('Redis', false)) {
	/**
	 * Class Redis
	 */
	class Redis
	{
		/**
		 *
		 */
		function connect()
		{
		}

		/**
		 *
		 */
		function select()
		{
		}

		/**
		 *
		 */
		function close()
		{
		}
	}
}
